package com.foton.logistic.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.foton.logistic.R;
import com.foton.logistic.base.BaseActivity;
import com.foton.logistic.listener.IOnResultListener;
import com.foton.logistic.model.SelectEntity;
import com.foton.logistic.task.DispatchTask;
import com.foton.logistic.task.ShowLoadTask;
import com.foton.logistic.task.TaskConstant;
import com.foton.logistic.util.BaseConstant;
import com.foton.logistic.util.OptionUtil;
import com.foton.logistic.util.net.HttpUtil;

import java.util.Map;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * Created by wuhaocong on 2017/9/13.
 */

public class AddShopActivity extends BaseActivity {
    @InjectView(R.id.person_info_user_text)
    TextView personInfoUserText;
    @InjectView(R.id.person_info_number_text)
    TextView personInfoNumberText;
    @InjectView(R.id.base_view_contain_layout)
    LinearLayout baseViewContainLayout;
    private String carrierId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ft_ui_add_shop);
        ButterKnife.inject(this);
    }

    @Override
    public void init() {
        setBackLayoutVisibility(View.VISIBLE);
        setTitleText("新增承运关系");
        setTitleTextVisibility(View.VISIBLE);
        registerBroad();
    }
    ReceiveBroadcast receiveBroadcast;
    private void registerBroad() {
        if (receiveBroadcast == null) {
            receiveBroadcast = new ReceiveBroadcast();
            IntentFilter filter = new IntentFilter();
            filter.addAction(BaseConstant.ACTION_SELECT_FILTER);
            this.registerReceiver(receiveBroadcast, filter);
        }
    }
    public class ReceiveBroadcast extends BroadcastReceiver {



        @Override
        public void onReceive(Context context, Intent intent) {
            try {
                String action = intent.getAction();
                if (action.equals(BaseConstant.ACTION_SELECT_FILTER)) {
                    SelectEntity selectEntity = (SelectEntity) intent.getSerializableExtra(BaseConstant.INTENT_SELECT);
                    carrierId =selectEntity.selectcarrierId;
                    personInfoUserText.setText(selectEntity.selectname);
                    personInfoNumberText.setText(selectEntity.selectnum);
                    //personInfoShopText.setText(selectEntity.selectname);
                   // SharedUtil.saveCarrierId1(SupplyActivity.this,carrierId);
                   /* if (recommendFilterEntity.type == 0) {
                        startTime = recommendFilterEntity.startTime;
                        endTime = recommendFilterEntity.endTime;
                        brandName =recommendFilterEntity.brand;
                        refresh(true);
                    }*/
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }
    @OnClick({R.id.person_info_user, R.id.person_info_num, R.id.ft_ui_find})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.person_info_user:
                Intent intent=new Intent(this,SelectActivity.class);
                startActivity(intent);
                break;
            case R.id.person_info_num:
                Intent intent1=new Intent(this,SelectActivity.class);
                startActivity(intent1);
                break;
            case R.id.ft_ui_find:
                requestData();
                break;
        }
    }

    private void requestData() {
        String url = BaseConstant.getCarrierAdd;
        Map<String, Object> argMap = HttpUtil.getEncryMap(AddShopActivity.this);
       // argMap.put("Id", list.get(position).Id);
        argMap.put("carrierId", carrierId);
        ShowLoadTask task = new ShowLoadTask(AddShopActivity.this, taskTag, baseViewContainLayout, loadLayout, "", false, null, url, argMap, TaskConstant.POST);
        // task.setParseClass(ShopResult.class);
        task.setiOnResultListener(new IOnResultListener() {
            @Override
            public void onOK(DispatchTask task) {
                //refresh(true);
                OptionUtil.addToast(AddShopActivity.this, "申请成功");
                finishSelf();
            }


            @Override
            public void onError(DispatchTask task) {

            }

            @Override
            public void onDone(DispatchTask task) {
                try {

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        task.execute();
    }



    @Override
    public void onDestroy() {
        try {
            this.unregisterReceiver(receiveBroadcast);
        } catch (Exception e) {
            e.printStackTrace();
        }
        super.onDestroy();
    }
}
